package com.datastructure.test.num2cn;

import java.util.HashMap;

public class NumTwoCn {

    public static void main(String[] args) {
        NumTwoCn numTwoCn = new NumTwoCn();
        String num2cn = numTwoCn.num2cn(1010011011);
        System.out.println(num2cn);
    }

    public String num2cn (int n) {
        // write code here
        boolean negative = false;
        if(n==0){
            return "零";
        }
        //变负为正
        if(n<0){
            n=Math.abs(n);
            negative=true;
        }
        StringBuilder stringBuilder = new StringBuilder();
        String res = String.valueOf(n);
        String[] splits = res.split("");
        int length = splits.length;
        HashMap<Integer,String> unitMap = new HashMap<>();
        unitMap.put(1,"十");
        unitMap.put(2,"百");
        unitMap.put(3,"千");
        unitMap.put(4,"万");
        unitMap.put(5,"万十");
        unitMap.put(6,"万百");
        unitMap.put(7,"万千");
        unitMap.put(8,"亿");
        unitMap.put(9,"亿十");
        HashMap<String,String> numberMap = new HashMap<>();
        numberMap.put("0","零");
        numberMap.put("1","一");
        numberMap.put("2","二");
        numberMap.put("3","三");
        numberMap.put("4","四");
        numberMap.put("5","五");
        numberMap.put("6","六");
        numberMap.put("7","七");
        numberMap.put("8","八");
        numberMap.put("9","九");
        for (int i = length - 1; i >= 0; i--) {
            String temp = splits[i];
            if(!temp.equals("0")){
                stringBuilder.append(unitMap.getOrDefault(length-i-1,""));
                stringBuilder.append(numberMap.get(temp));
            } else {
                stringBuilder.append("零");
            }
        }
        while (stringBuilder.charAt(0)=='零'){
            stringBuilder.deleteCharAt(0);
        }
        for (int i = stringBuilder.length()-1; i >=0; i--) {
            if(stringBuilder.charAt(i)=='零'&&stringBuilder.charAt(i+1)=='零'){
                stringBuilder.deleteCharAt(i);
            }
        }
        //去掉最前面的一
        if(stringBuilder.charAt(stringBuilder.length()-1)=='一'&&
                stringBuilder.charAt(stringBuilder.length()-2)=='十'){
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
        if(negative){
            stringBuilder.append("负");
        }
        StringBuilder reverseStringBuilder = stringBuilder.reverse();
        //去掉多余的亿
        int index = 0;
        for (int i = 0; i < reverseStringBuilder.length(); i++) {
            if(reverseStringBuilder.charAt(i)=='亿'){
                index++;
            }
        }
        while (index>1){
            for (int i = 0; i < reverseStringBuilder.length(); i++) {
                if(reverseStringBuilder.charAt(i)=='亿'){
                    reverseStringBuilder.deleteCharAt(i);
                    break;
                }
            }
            index--;
        }
        //去掉多余的万
        index = 0;
        for (int i = 0; i < reverseStringBuilder.length(); i++) {
            if(reverseStringBuilder.charAt(i)=='万'){
                index++;
            }
        }
        while (index>1){
            for (int i = 0; i < reverseStringBuilder.length(); i++) {
                if(reverseStringBuilder.charAt(i)=='万'){
                    reverseStringBuilder.deleteCharAt(i);
                    break;
                }
            }
            index--;
        }
        return reverseStringBuilder.toString();
    }
}
